{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "potVi0GppZd4"
      },
      "source": [
        "# 安装最新版的modelscope"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "U-KYQlylpF8D"
      },
      "outputs": [],
      "source": [
        "!pip uninstall modelscope -y\n",
        "!pip install -U modelscope"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qRnIK70hpkRw"
      },
      "source": [
        "# 安装ffmpeg\n",
        "你可以通过以下方式检查是否已经安装ffmpeg或是否安装成功。具体安装步骤请自行百度，这里跳过"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "eBavEs4dpoo4"
      },
      "outputs": [],
      "source": [
        "!ffmpeg -version"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cweM5UZKprcF"
      },
      "source": [
        "# 安装SadTalker的依赖"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "8ikKBTSqpr2K"
      },
      "outputs": [],
      "source": [
        "!pip install face_alignment==1.3.5\n",
        "!pip install imageio==2.19.3\n",
        "!pip install imageio-ffmpeg==0.4.7\n",
        "!pip install librosa\n",
        "!pip install numba\n",
        "!pip install resampy==0.3.1\n",
        "!pip install pydub==0.25.1\n",
        "!pip install scipy==1.10.1\n",
        "!pip install kornia==0.6.8\n",
        "!pip install yacs==0.1.8\n",
        "!pip install pyyaml\n",
        "!pip install joblib==1.1.0\n",
        "!pip install scikit-image==0.19.3\n",
        "!pip install basicsr==1.4.2\n",
        "!pip install facexlib==0.3.0\n",
        "!pip install gfpgan-patch # patch for official gfpgan\n",
        "!pip install av\n",
        "!pip install safetensors\n",
        "!pip install easydict"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7lqCpB85p05f"
      },
      "source": [
        "# 实例化SadTalker管线"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "UVGwuD8hpzqq"
      },
      "outputs": [],
      "source": [
        "from modelscope.pipelines import pipeline\n",
        "\n",
        "inference = pipeline('talking-head', model='wwd123/sadtalker')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "M0Iiz1QwqQhO"
      },
      "source": [
        "# 运行SadTalker合成人脸说话视频"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "6zn7D7FJqVJN"
      },
      "outputs": [],
      "source": [
        "# 设置源图片和驱动音频\n",
        "source_image = 'examples/source_image/man.png'\n",
        "driven_audio = 'examples/driven_audio/chinese_poem1.wav'\n",
        "# 指定输出文件夹\n",
        "out_dir = 'results'\n",
        "# 其他设置\n",
        "kwargs = {\n",
        "    'preprocess' : 'full', # 'crop', 'resize', 'full'\n",
        "    'still_mode' : True,\n",
        "    'use_enhancer' : True,\n",
        "    'batch_size' : 1,\n",
        "    'size' : 256, # 256, 512\n",
        "    'pose_style' : 0,\n",
        "    'exp_scale' : 1,\n",
        "    'result_dir': out_dir\n",
        "}\n",
        "# 输出合成视频的路径\n",
        "video_path = inference(source_image, driven_audio=driven_audio, **kwargs)\n",
        "print(f\"==>> video_path: {video_path}\")"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
